package com.ayf.payment.game.api.controller;

import cn.hutool.core.collection.CollUtil;
import com.ayf.payment.game.api.dto.GameInfoDTO;
import com.ayf.payment.game.api.dto.GameInfoGiftDTO;
import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.dto.criteria.GameInfoCriteria;
import com.ayf.payment.game.api.dto.criteria.GameInfoPageCriteria;
import com.ayf.payment.game.api.dto.platform.NetConfigDTO;
import com.ayf.payment.game.api.exception.BusinessException;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.GameInfoGiftService;
import com.ayf.payment.game.api.service.GameInfoService;
import com.ayf.payment.game.api.service.platform.NetConfigService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 游戏信息控制层
 * @Author: zh-liang
 * @Date : 2019-12-27 14:46
 */
@RestController
@RequestMapping("/v/gameInfo")
public class GameInfoController {
    @Autowired
    private GameInfoService gameInfoService;
    @Autowired
    private GameInfoGiftService gameInfoGiftService;
    @Autowired
    private NetConfigService netConfigService;

    @PostMapping("/add")
    public CommonResult<Boolean> add(MerchantDTO merchantDTO, @RequestBody GameInfoCriteria gameInfo) {
        if (merchantDTO == null) {
            throw new BusinessException("请登录");
        }
        if (gameInfo == null) {
            throw new ParameterException("游戏信息不能为空");
        }
        gameInfo.setMerchantId(merchantDTO.getId());
        if (gameInfoService.insert(gameInfo)) {
            return new CommonResult<Boolean>(CommonConstants.RETURN_SUCCESS, "添加游戏信息成功", true);
        }
        return new CommonResult<Boolean>(CommonConstants.RETURN_ERROR, "添加游戏信息失败", false);
    }

    @PostMapping("/del")
    public CommonResult del(MerchantDTO merchantDTO, @RequestBody GameInfoCriteria gameInfo) {

        if (gameInfo == null || gameInfo.getId() == null) {
            throw new ParameterException("请选择游戏记录！");
        }
        if (gameInfoService.del(gameInfo.getId(), merchantDTO.getId())) {
            return new CommonResult(CommonConstants.RETURN_SUCCESS, "删除成功");
        } else {
            return new CommonResult(CommonConstants.RETURN_ERROR, "删除失败");
        }

    }

    /**
     * 开区助手专用游戏名称查询接口
     * @param merchantDTO
     * @param gameInfo
     * @return
     */
    @PostMapping("/selectPage")
    public CommonResult<PageInfo> selectPage(MerchantDTO merchantDTO, @RequestBody GameInfoCriteria gameInfo) {
        if (merchantDTO == null) {
            throw new BusinessException("请登录");
        }
        gameInfo.setMerchantId(merchantDTO.getId());
        List<GameInfoDTO> list = gameInfoService.select(gameInfo);
        PageInfo pageInfo = new PageInfo(list);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "查询游戏信息成功", pageInfo);
    }

    /**
     * 平台使用的游戏名称查询列表
     *
     * @param merchantDTO
     * @param gameInfo
     * @return
     */
    @PostMapping("/selectPage2")
    public CommonResult<PageInfo> selectPage2(MerchantDTO merchantDTO, @RequestBody GameInfoPageCriteria gameInfo) {
        if (merchantDTO == null) {
            throw new BusinessException("请登录");
        }
        gameInfo.setMerchantId(merchantDTO.getId());
        PageHelper.startPage(gameInfo.getPageNum(), gameInfo.getPageSize());
        List<GameInfoDTO> list = gameInfoService.selectPage(gameInfo);
        PageInfo pageInfo = new PageInfo(list);
        return new CommonResult<PageInfo>(CommonConstants.RETURN_SUCCESS, "查询游戏信息成功", pageInfo);
    }

    @PostMapping("/select")
    public CommonResult<List<GameInfoDTO>> select(MerchantDTO merchantDTO, @RequestBody GameInfoCriteria gameInfo) {
        if (merchantDTO == null) {
            throw new BusinessException("请登录");
        }
        gameInfo.setMerchantId(merchantDTO.getId());
        List<GameInfoDTO> list = gameInfoService.select(gameInfo);
        return new CommonResult<List<GameInfoDTO>>(CommonConstants.RETURN_SUCCESS, "查询游戏信息成功", list);
    }

    @PostMapping("/selectOne")
    public CommonResult<GameInfoDTO> selectOne(MerchantDTO merchant, @RequestBody GameInfoCriteria gameInfo) {
        if (merchant == null) {
            throw new BusinessException("请登录");
        }
        if (gameInfo == null || StringUtils.isBlank(gameInfo.getId())) {
            throw new ParameterException("游戏信息不能为空");
        }
        GameInfoDTO info = gameInfoService.selectOne(gameInfo.getId());
        if (StringUtils.isNotBlank(info.getImg())) {
            NetConfigDTO netConfig = netConfigService.getNetConfig();
            info.setImgUrl(netConfig.getDomainName() + "/gameapip/v/gameGift/downloadImg?fileName=" + info.getImg());
        }
        List<GameInfoGiftDTO> list1 = gameInfoGiftService.selectByGameInfoId(info.getId());
        if (CollUtil.isEmpty(list1)) {
            list1 = new ArrayList<>();
            GameInfoGiftDTO infoGiftDTO = GameInfoGiftDTO.builder().startTime("00:00:00").endTime("00:00:00").build();
            list1.add(infoGiftDTO);
        }
        info.setTotal(list1.get(0).getTotal());
        info.setAmount(list1.get(0).getAmount());
        info.setGameCurrency(list1.get(0).getGameCurrency());
        info.setTimeList(list1);
        return new CommonResult<GameInfoDTO>(CommonConstants.RETURN_SUCCESS, "查询游戏信息成功", info);
    }

    @PostMapping("/update")
    public CommonResult<Boolean> update(MerchantDTO merchant, @RequestBody GameInfoCriteria gameInfo) {
        if (merchant == null) {
            throw new BusinessException("请登录");
        }
        if (gameInfo == null) {
            throw new ParameterException("游戏信息不能为空");
        }
        if (gameInfoService.update(gameInfo)) {
            return new CommonResult<Boolean>(CommonConstants.RETURN_SUCCESS, "修改游戏信息成功", true);
        }
        return new CommonResult<Boolean>(CommonConstants.RETURN_ERROR, "修改游戏信息失败", false);
    }

    /**
     * 修改排序序号
     * @param criteria
     * @return
     */
    @PostMapping("/updateSort")
    public CommonResult updateSort(@RequestBody GameInfoCriteria criteria){
        gameInfoService.updateSort(criteria);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, "");
    }
}
